<?php 
/**
 * 物流应付款统计
 * @copyright   Copyright (c) 2006 - 2010 DZH 友拓软件友拓通
 * @category   	账目信息
 * @package  	Model
 * @author    	李恺弘
 * @version 	2.1,2012-07-22
 */

class LogisticsStatPublicModel extends AbsStatPublicModel { 
	 
	public $comp_type=3; 
	///字段列表
	private $sqlFields	=	'';		
	
	/**
	 * 取列表数据
	 *
	 * @param  array $info 查询参数
	 * @return array
	 */
	public function getIndex() {
		if (!empty($_POST['date']['from_paid_date']) && !empty($_POST['date']['to_paid_date'])){
			$money = '(should_paid)*income_type'; 
			$having = $_POST['show']==1 ? '' : ' having(sum('.$money.')!=0)';	
			$s_where = 'object_type!=329';		
		}else{
			if ($_POST['date']['lt_paid_date']) {
				$money = '(original_money+discount_money)*income_type';
				$s_where = 'object_type!=329';			
			} else {
				$money = 'need_paid*income_type';
				$s_where = $_POST['show']==1 ? '( is_close=0 or ( is_close>0 and object_type=303 and object_type_extend=3 ) )' : 'is_close=0 and need_paid!=0';  
			}
			if(!$_POST['show']) { /// 不显示己结清的物流
				$having = " having sum(".$money.") !=0";
			}			
		}

		$where = _search($s_where);			
		$sql_field 	= ' select *,-sum('.$money.') as total_need_paid	'.$this->fundsField().'			
				from logistics_paid_detail 			
				where  '.$where.' ';
		$sql_group	= '	group by comp_id,currency_id '.$having.'
				order by comp_id';	
		$sql_group_total	= '	group by currency_id '.$having.' ';
		$sql				=	$sql_field.$sql_group;	 
		$list	= _listStat($sql);  				
///		$list = $this->db->query($sql);	
		if (empty($list)) return ;	
		$list = _formatList($list);
		///区间查询显示
		if (!empty($_POST['date']['from_paid_date']) && !empty($_POST['date']['to_paid_date'])){
			$url_date = '/type/2/from_paid_date/'.formatDate($_POST['date']['from_paid_date']).'/to_paid_date/'.formatDate($_POST['date']['to_paid_date']); 
		}else{
			///非区间查询显示
			if(empty($_POST['date']['lt_paid_date'])){
				$url_date = ''; 
			}else {
				$url_date = '/type/2/to_paid_date/'.formatDate($_POST['date']['lt_paid_date']); 
			}			
		} 
		foreach ($list['list'] as $key => &$value) {
			$value['detail_url'] = U('/LogisticsStat/view/comp_id/'.$value['comp_id'].
							'/currency_id/'.$value['currency_id'].$url_date);
		}	 
		$have_currency_name	=	C('logistics_currency_count')>1?true:false;
		$list	=	$this->getTotalCurrencyInfo(&$list,array('total_need_paid'),1,$have_currency_name); 
		
		///合计为分页的总数量
		$sql_total		=	$sql_field.$sql_group_total;
		$detail_total 	= _formatList($this->db->query($sql_total));   
		$detail_total	= $this->getTotalCurrencyInfo(&$detail_total,array('total_need_paid'),1,$have_currency_name); 
		$list['page_total']	= $detail_total; 
		
		return $list;			
	} 		
	
	/**
	 * 取列表数据
	 *
	 * @return array
	 */
	public function getDetail() { 
		$info	=	$_GET;
		if ($info['comp_id']>0){
			$cache_info	=	SOnly('company_expand',intval($info['comp_id'])); 
			$today = date("Y-m-d");
			if ($cache_info['remind_day']>0){
				$this->sqlFields	=	' ,if(datediff(\''.$today.'\',paid_date)>'.$cache_info['remind_day'].' && income_type=-1 and need_paid>0 and is_close=0 ,2,1) as tr_color	';
			}else{
				$this->sqlFields	=	' ,1 as tr_color	';
			} 
		}
		///区间查询
		if (!empty($info['from_paid_date']) && !empty($info['to_paid_date'])){
			$query_info['date']['from_paid_date'] = $_GET['from_paid_date'];
			$query_info['date']['to_paid_date']   = $_GET['to_paid_date'];
			$_GET['type'] = 4;
			unset($info['from_paid_date']);
			unset($info['to_paid_date']);
		}else{
			///非区间查询 
			if ($info['to_paid_date']) {
				$query_info['date']['lt_paid_date'] = $_GET['to_paid_date'];
				empty($_GET['type']) && $_GET['type'] = 3;
				unset($info['to_paid_date']);
			}else {
				empty($_GET['type']) && $_GET['type'] = 1;
			}		
		}  
		unset($info['type']);
		$query_info['query']	= $info;	 
		$functions	=	array(								
								'1'=>'UnPaidList',///未结清的款项明细
								'2'=>'AllList',///全部欠款明细
								'3'=>'afterCloseOutList',///平账后的所有款项信息
								'4'=>'afterCloseOutBetweenList',///平账后的所有款项信息(两个日期区间)
		);		
		$function_name		= $functions[$_GET['type']];   ///根据流程类型 得到函数名	
		if (method_exists($this,$function_name)) {
			$list 	= $this->$function_name($query_info);
		} 
		return $list; 
	} 		
	 
	/**
	 * 未结清的款项
	 *
	 * @param array $info 查询参数
	 * @return array
	 */
	public function UnPaidList($info) {	 
		$where  = getWhere($info);	
		$sql	= 'select * '.$this->sqlFields.'			
					from logistics_paid_detail 
					where  is_close=0 and need_paid!=0 and'.$where.' 
					order by is_close desc ,paid_date asc,paid_id asc';		
		$list	=	_listStat($sql); 		   
		$sql_total = 'select 
			sum(if(income_type<0,original_money,0)) as original_money,
			sum(discount_money) as discount_money,
			sum(should_paid) as should_paid,
			sum(if(income_type<0,have_paid,original_money)) as have_paid,
			sum(if(income_type<0,need_paid,have_paid)) as need_paid,
			sum(if(income_type>0,need_paid,0)) as use_paid, 
			sum(need_paid*income_type*-1) as money
		from logistics_paid_detail 
					where  is_close=0 and need_paid!=0 and'.$where.' ';
		$totalList	= _formatList($this->db->query($sql_total));  
		
		if (empty($list)) return ;	
		$sum_need_paid = 0;		
		foreach ($list as &$value) {
			if ($value['income_type']==1) { ///付款
				$sum_need_paid 			-= $value['need_paid'];
				$value['sum_need_paid']	= $sum_need_paid;
				$value['use_paid']		= $value['need_paid'];
				$value['have_paid'] 	= $value['original_money'];
				$value['need_paid']		= $value['original_money'] = 0;				
			}else { /// 欠款
				$value['use_paid']		= 0;
				$sum_need_paid 			+= $value['need_paid'];
				$value['sum_need_paid']	= $sum_need_paid;
			}	
			/// 生成备注
			$value = $this->objectTypeCommentSubsidiary($value);			
		}
		$list	= _formatList($list);
		$list['total']['total_sum_need_paid'] = moneyFormat($value['sum_need_paid'], 0, C('MONEY_LENGTH'));				
		$list['all_total']					  = $totalList['list'][0];  	///合计	   
		return $list;
	} 
	
	/**
	 * 平账后的所有款项信息
	 *
	 * @param array $info 查询参数
	 * @return array
	 */
	public function afterCloseOutList($info) {
		extract($info['query']);
		$where	= getWhere($info);
		///没有日期查询显示对账后的信息
 		if (empty($info['date']['lt_paid_date']) && empty($info['date']['from_paid_date']) && empty($info['date']['from_paid_date']) ){
 			$where .= ' and is_close=0';
 		} 
		$sql = 'select * '.$this->sqlFields.'			 
				from logistics_paid_detail 
				where  should_paid!=0 and '.$where.'
				order by paid_date asc,paid_id asc';	
		$list	=	_listStat($sql); 	

		$sql_total = 'select 
			sum(if(income_type<0,original_money,0)) as original_money,
			sum(discount_money) as discount_money,
			sum(should_paid) as should_paid,
			sum(if(income_type<0,0,original_money)) as have_paid,
			sum(if(income_type<0,need_paid,have_paid)) as need_paid,
			sum(if(income_type>0,need_paid,0)) as use_paid, 
			sum(need_paid*income_type*-1) as money
		from logistics_paid_detail 
				where  should_paid!=0 and '.$where.' ';
		$totalList	= _formatList($this->db->query($sql_total));  
		
		 
		if (empty($list)) return ;	
		foreach ($list as &$value) {
			if ($value['income_type']==1) { ///付款
				$sum_need_paid 			-= $value['should_paid'];
				$value['sum_need_paid']	= $sum_need_paid;				
				$value['have_paid'] 	= $value['original_money'];
				$value['need_paid']		= $value['original_money'] = 0;				
			}else {	/// 欠款			
				$sum_need_paid 			+= $value['should_paid'];
				$value['have_paid']		 = 0;
				$value['sum_need_paid']	= $sum_need_paid;
			}	
			/// 生成备注
			$value = $this->objectTypeCommentSubsidiary($value);			
		}					
		$list = _formatList($list);	
		$list['total']['total_sum_need_paid'] = moneyFormat($value['sum_need_paid'], 0, C('MONEY_LENGTH'));				
		$list['all_total']					  = $totalList['list'][0];  	///合计	   
		return $list;
	}	
 
	/**
	 * 平账后的所有款项信息
	 *
	 * @param array $info 查询参数
	 * @return array
	 */
	public function afterCloseOutBetweenList($info) {
		extract($info['query']);
		$where	= getWhere($info);
		$where	.=' and object_type!=329 ';
		///没有日期查询显示对账后的信息
 		if (empty($info['date']['lt_paid_date']) && empty($info['date']['from_paid_date']) && empty($info['date']['from_paid_date']) ){
 			$where .= ' and is_close=0';
 		} 
		$sql = 'select * '.$this->sqlFields.'			 
				from logistics_paid_detail 
				where  should_paid!=0 and '.$where.'
				order by paid_date asc,paid_id asc';	
		$list	=	_listStat($sql); 

		$sql_total = 'select 
			sum(if(income_type<0,original_money,0)) as original_money,
			sum(discount_money) as discount_money,
			sum(should_paid) as should_paid,
			sum(if(income_type<0,0,original_money)) as have_paid,
			sum(if(income_type<0,need_paid,have_paid)) as need_paid,
			sum(if(income_type>0,need_paid,0)) as use_paid, 
			sum(need_paid*income_type*-1) as money
		from logistics_paid_detail 
				where  should_paid!=0 and '.$where.' ';
		$totalList	= _formatList($this->db->query($sql_total));  
		
		 
		if (empty($list)) return ;	
		foreach ($list as &$value) {
			if ($value['income_type']==1) { ///付款
				$sum_need_paid 			-= $value['should_paid'];
				$value['sum_need_paid']	= $sum_need_paid;				
				$value['have_paid'] 	= $value['original_money'];
				$value['need_paid']		= $value['original_money'] = 0;				
			}else {	/// 欠款			
				$sum_need_paid 			+= $value['should_paid'];
				$value['have_paid']		 = 0;
				$value['sum_need_paid']	= $sum_need_paid;
			}	
			/// 生成备注
			$value = $this->objectTypeCommentSubsidiary($value);			
		}					
		$list = _formatList($list);	
		$list['total']['total_sum_need_paid'] = moneyFormat($value['sum_need_paid'], 0, C('MONEY_LENGTH'));				
		$list['all_total']					  = $totalList['list'][0];  	///合计	   
		return $list;
	}	
	
	/**
	 * 欠款明细分组显示(全部欠款明细)
	 *
	 * @param array $info 查询参数
	 * @return array 
	 */
	public function AllList($info) {
		/// 对帐及平帐
		$where  = 	getWhere($info);					
		$sql	= ' select * '.$this->sqlFields.' from (
					select *,if(is_close=0, 99999, is_close) as order_is_close
		 			from logistics_paid_detail 
		 			where  should_paid !=0 and  '.$where.'
		 			) b 
		 			order by order_is_close asc,paid_date asc,paid_id asc';		
		$list	=	_listStat($sql); 	

		$sql_total = 'select 
			sum(if(income_type<0,original_money,0)) as original_money,
			sum(discount_money) as discount_money,
			sum(should_paid) as should_paid,
			sum(if(income_type<0,0,original_money)) as have_paid,
			sum(if(income_type<0,need_paid,have_paid)) as need_paid,
			sum(if(income_type>0,need_paid,0)) as use_paid, 
			sum(need_paid*income_type*-1) as money
		from (
					select *,if(is_close=0, 99999, is_close) as order_is_close
		 			from logistics_paid_detail 
		 			where  should_paid !=0 and  '.$where.'
		 			) b  ';
		$totalList	= _formatList($this->db->query($sql_total));  
			
		if (empty($list)) return ;		
		$is_close = 0;	
		foreach ($list  as   &$value) {
			if ($is_close!=$value['is_close']) {
				$sum_need_paid 	= 0;
				!$value['is_close'] && $temp[$value['is_close']] = $temp[$is_close];	
				$is_close		= $value['is_close'];														
			}						
			if (in_array($value['object_type'], array(304,329))) {					
				$temp[$is_close] = array('paid_date' => $value['paid_date'], 'object_type' => $value['object_type']);	
			}			
			if ($value['income_type']==1) {/// 付款
				$value['have_paid'] 		= $value['original_money'];
				$value['original_money'] 	= 0;
				$sum_need_paid 				-= $value['should_paid'];
				$value['sum_need_paid']		= $sum_need_paid;
			}else { /// 欠款
				$value['have_paid'] 		= 0;
				$sum_need_paid 				+= $value['should_paid'];
				$value['sum_need_paid']		= $sum_need_paid;
			}	
			/// 生成备注
			$value = $this->objectTypeCommentSubsidiary($value);						
			/// 合计部分
			$sum[$is_close]['total_original_money'] += $value['original_money'];
			$sum[$is_close]['total_have_paid'] 		+= $value['have_paid'];
			$sum[$is_close]['total_discount_money'] += $value['discount_money'];
			$sum[$is_close]['sum_need_paid'] 		= $sum_need_paid;
		}				
		$list = _formatList($list);		
		$total_sum_need_paid = moneyFormat($value['sum_need_paid'], 0, C('MONEY_LENGTH'));			
		foreach ($list['list'] as $k =>  &$value)	{
			$value['total_original_money'] = moneyFormat($sum[$value['is_close']]['total_original_money'],0,C('MONEY_LENGTH'));
			$value['total_have_paid']	   = moneyFormat($sum[$value['is_close']]['total_have_paid'],0,C('MONEY_LENGTH'));
			$value['total_discount_money'] = moneyFormat($sum[$value['is_close']]['total_discount_money'],0,C('MONEY_LENGTH'));	
			$value['total_sum_need_paid']  = moneyFormat($sum[$value['is_close']]['sum_need_paid'],0,C('MONEY_LENGTH'));			
			/// 生成分组明细的标题
			if (empty($temp)) { /// 无平帐
				$value['title'] = $value['comp_name'].$value['dd_currency_id'].L('arrearage_details');
			} else { /// 有平帐
				$ext_title = $temp[$value['is_close']]['object_type'] == 304 ? L('closeout') : L('checkout');				
				$ext_title.= !$value['is_close'] ? L('aft_arrearage_details') : L('bef_arrearage_details');			
				$value['title'] = $value['comp_name'].$value['dd_currency_id'].formatDate($temp[$value['is_close']]['paid_date'], 'outdate').$ext_title;
			}
			$list['group_list'][$value['is_close']][]	=	$value;
		}	
		$list['all_total']					  			= $totalList['list'][0];  	///合计	   	
		return $list;	
	} 
}